package com.andy.blog.admin.service.impl;

import com.andy.blog.admin.dto.AlbumDTO;
import com.andy.blog.admin.service.AlbumBusService;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.AlbumDO;
import com.andy.blog.dal.service.AlbumDalService;
import com.andy.blog.dal.vo.AlbumVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/10/15 16:25
 */
@Service
public class AlbumBusServiceImpl implements AlbumBusService {

    @Resource
    private AlbumDalService albumDalService;

    @Override
    public List<AlbumDTO> listAlbumVO() {

        List<AlbumDO> albumDOS = albumDalService.selectList(new AlbumVO());

        return TransformUtils.transformList(albumDOS, AlbumDTO.class);
    }

    @Override
    public void add(AlbumVO albumVO) {
        if (Objects.isNull(albumVO) || StringUtils.isAnyBlank(albumVO.getAlbumDesc(),
                albumVO.getAlbumName(), albumVO.getAlbumCover(), albumVO.getAlbumType())) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        // TODO 校验相册类型是否存在

        albumDalService.insertData(albumVO);
    }

    @Override
    public PageUtilsDTO<AlbumDTO> selectPage(AlbumVO albumVO) {
        IPage<AlbumDO> albumDOIPage = albumDalService.selectPage(albumVO);
        return PageUtilsDTO.build(albumDOIPage.getTotal(), TransformUtils.transformList(albumDOIPage.getRecords(), AlbumDTO.class));
    }

    @Override
    public void updateById(AlbumVO albumVO) {
        if (Objects.isNull(albumVO) || StringUtils.isAnyBlank(albumVO.getAlbumDesc(), albumVO.getAlbumType(),
                albumVO.getAlbumName(), albumVO.getAlbumCover())) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        albumDalService.update(albumVO);
    }
}
